﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WorkFlowSystem.Common;
using WorkFlowSystem.DataBase;

namespace WorkFlowSystem
{
    public partial class UserSettingUi : Form
    {
        public UserSettingUi()
        {
            InitializeComponent();
            m_pwd_max_length = 64;
            m_pwd_min_length = 6;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string old_pwd = textBoxX1.Text;
            string new_pwd = textBoxX2.Text;
            string new_pwd_confirm = textBoxX3.Text;
            string pwd_question = textBoxX4.Text;
            string pwd_answer = textBoxX5.Text;

            RetCode err = validator(old_pwd, new_pwd, new_pwd_confirm, pwd_question, pwd_answer);

            switch (err) { 
                case RetCode.MSG_CHGPSW_UPDATE_SUCCESS:
                    MessageBox.Show("密码设置成功");
                    this.Close();
                    break;
                case RetCode.MSG_CHGPSW_NOT_NULL:
                    MessageBox.Show("密码不能为空");
                    break;
                case RetCode.MSG_CHGPSW_NEW_OLD_ERROR:
                    MessageBox.Show("新密码两次输入不一致");
                    break;
                case RetCode.MSG_CHGPSW_LENGTH_6_U:
                    MessageBox.Show("密码长度不能小于6个字符");
                    break;
                case RetCode.MSG_CHGPSW_TYPE:
                    MessageBox.Show("请使用数字和英文字母");
                    break;
                case RetCode.MSG_CHGPSW_OLD_ERROR:
                    MessageBox.Show("原密码错误");
                    break;
                case RetCode.MSG_UNKNOWN:
                    break;
                default:
                    Logger.WriteLog("un-expected error [" + err.ToString() + "]");
                    break;
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            CommonDefine.clear_values(this.panelEx1);
        }

        private RetCode validator(string old_pwd, string new_pwd, string new_pwd_confirm, string pwd_question, string pwd_answer)
        {
            if (!CommonValidate.inputNullValidate(old_pwd) ||
                !CommonValidate.inputNullValidate(new_pwd) ||
                !CommonValidate.inputNullValidate(new_pwd_confirm))
            {
                return RetCode.MSG_CHGPSW_NOT_NULL;
            }

            if (new_pwd != new_pwd_confirm)
            {
                return RetCode.MSG_CHGPSW_NEW_OLD_ERROR;
            }

            if (!CommonValidate.inputLenValidate(new_pwd, m_pwd_min_length, m_pwd_max_length) ||
                !CommonValidate.inputLenValidate(new_pwd_confirm, m_pwd_min_length, m_pwd_max_length))
            {
                return RetCode.MSG_CHGPSW_LENGTH_6_U;
            }

            if (!CommonValidate.inputEngNumLenValidate(new_pwd, m_pwd_min_length, m_pwd_max_length) ||
                !CommonValidate.inputEngNumLenValidate(new_pwd_confirm, m_pwd_min_length, m_pwd_max_length))
            {
                return RetCode.MSG_CHGPSW_TYPE;
            }

            //获取原密码
            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return RetCode.MSG_UNKNOWN;
            }
            string sql = "select u_password from user_login where uid = @uid";
            List<SqlParameter> para_list = new List<SqlParameter>();
            para_list.Add(new SqlParameter("@uid", CurrentUser.user_id));

            string u_password = "";
            object first_cell = new object();
            if (db_op.ExecuteScalar(sql, para_list, false, ref first_cell) < 0)
            {
                db_op.CloseDB();
                return RetCode.MSG_UNKNOWN;
            }
            db_op.CloseDB();

            //验证密码
            u_password = first_cell.ToString();
            if (u_password != old_pwd)
            {
                MessageBox.Show("原密码输入错误");
                return RetCode.MSG_CHGPSW_OLD_ERROR;
            }

            //更新密码
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return RetCode.MSG_UNKNOWN;
            }
            sql = "update user_login set u_password=@u_password, u_question=@u_question, u_answer=@u_answer, u_update_uid=@update_id, u_update_date=@update_date where uid = @uid";
            para_list.Clear();
            para_list.Add(new SqlParameter("@uid", CurrentUser.user_id));
            para_list.Add(new SqlParameter("@u_password", new_pwd));
            para_list.Add(new SqlParameter("@u_question", pwd_question));
            para_list.Add(new SqlParameter("@u_answer", pwd_answer));
            para_list.Add(new SqlParameter("@update_id", CurrentUser.user_id));
            para_list.Add(new SqlParameter("@update_date", DateTime.Now));

            if (db_op.ExecuteNonQuery(sql, para_list, false) < 0)
            {
                MessageBox.Show("更改密码失败");
                db_op.CloseDB();
                return RetCode.MSG_UNKNOWN;
            }
            db_op.CloseDB();

            return RetCode.MSG_CHGPSW_UPDATE_SUCCESS;
        }

        private int m_pwd_min_length;
        private int m_pwd_max_length;
    }
}
